WEBVTT

00:00.700 --> 00:04.310
Hello and welcome back in the session about for loops of follow ups.

00:04.360 --> 00:09.440
One of the most important and most heavily used concepts and programming.

00:09.610 --> 00:15.100
And before we learned about lists and lists that contain us of many other objects so like a backpack

00:15.440 --> 00:21.580
that you put in things like a sandwich or a bottle of water or an extra T-shirt before you go hiking.

00:21.580 --> 00:28.240
And now let's assume that it's raining in your backpack and all elements get wet and what do I typically

00:28.240 --> 00:31.510
do you unpack each element and take an action on it.

00:31.510 --> 00:37.910
So in this case you are trying each element in the Sun and others call it an elemental ICE operation.

00:37.960 --> 00:44.500
But earlier we learned that however element vice operations are not a stand out behavior of lists.

00:44.560 --> 00:46.720
So let's create a list with four elements.

00:46.720 --> 00:48.020
One two three four.

00:48.040 --> 00:54.610
And now our attention is to multiply each element by two or so an element y's operation but you already

00:54.610 --> 01:01.630
know if you just put added times to we get a multiple of the list that set off so actually we created

01:01.660 --> 01:07.720
two copies of L and then we concatenated it but we didn't get an element y's operation.

01:07.780 --> 01:13.930
But now what should we do if we want to f an element y's operation and they are the far keyword comes

01:13.930 --> 01:14.580
into place.

01:14.620 --> 01:20.830
So we are saying for j in lists that we can also say for element in lists

01:25.150 --> 01:29.020
we can name the each element of the list with the name we want.

01:29.020 --> 01:35.890
We can say element or i or j and that's important so we can say for element for each element and list

01:35.920 --> 01:37.780
and then we have to make a colon.

01:38.320 --> 01:43.330
And if you print that and if you press return then we automatically pass and make serial indent and

01:43.330 --> 01:51.950
then we say print the element so we can see here we iterated over all elements and we printed all elements

01:51.960 --> 01:53.700
so we get one two three four.

01:53.740 --> 01:59.380
And of course we can do more than just printing we can say okay each element we want to multiply by

01:59.380 --> 02:01.240
two and then print it.

02:01.270 --> 02:02.290
We can also do that

02:05.450 --> 02:12.770
and also we can say that we name each element either Multiply each side by two and then rename the multiplied

02:12.800 --> 02:17.400
element as J and then print Chase so we get the same result of course.

02:17.540 --> 02:24.260
And before we learned the function length so with the function length we can determine the amount of

02:24.260 --> 02:30.230
elements and unless we can replicate this process by a few lines of code so we can say count equals

02:30.230 --> 02:35.810
zero and four element or four under slash in list please increase my count

02:38.450 --> 02:39.500
sum count as far.

02:39.500 --> 02:44.190
So we just determined the length of our list L four elements.

02:44.530 --> 02:48.020
Again I one wanted to highlight two things so you need the colon here.

02:48.050 --> 02:56.500
So it's the same with an if statement if you forget the colon you get an error message and we need to.

02:56.520 --> 02:57.270
The intent here.

02:57.270 --> 03:00.230
So if you forget the intent we also gotten our message

03:03.580 --> 03:05.570
though this is still the right structure.

03:05.580 --> 03:12.810
So before I said we can iterate over all its rebels Soviet era appearance objects that contain many

03:12.900 --> 03:16.580
elements and also we learned that a string also is a contain of many elements.

03:16.580 --> 03:21.930
So for example the string dock contains three characters d o a..

03:22.110 --> 03:29.640
And they let us create the rival D equal stock and then we can also iterate over D and print all single

03:29.640 --> 03:32.690
element D O T.

03:33.090 --> 03:39.360
And we can also print each character as an upper character so we can say for each character in D please

03:39.360 --> 03:47.370
print me in the upper character and we get D O T and we can also determine the length of our string

03:47.670 --> 03:49.110
and of course it's three

03:51.930 --> 03:57.180
at this point I want to introduce the range object and arrange object is also an interval so we can

03:57.180 --> 04:05.730
iterate over all elements and in this example the range object stops integers from 1 including until

04:05.910 --> 04:15.360
5 excluding So if you're under so you only get here the range object from 1 to 5 and we can also check

04:15.360 --> 04:21.720
the tape of the range object its range object and when I say the range object stops then the elements

04:21.720 --> 04:28.020
it's the action of really storing it it just makes itself ready to give me all elements if I explicitly

04:28.410 --> 04:33.900
want to have them so I can explicitly call the range object by creating a list

04:37.100 --> 04:42.870
and then the list is containing all four elements of our range object we can also iterate over arrange

04:42.900 --> 04:51.420
objects or same with the list so we can say for Aeon range 1 to 5 please print me all elements and we

04:51.420 --> 04:52.730
get 1 2 3 4.

04:52.740 --> 05:00.120
So the last element excluding So from 1 including 2 5 excluding and this is actually the same behavior

05:00.150 --> 05:03.990
as the list so before we had our list.

05:04.020 --> 05:10.590
1 2 3 4 and we iterated over it and we got all the elements.

05:10.590 --> 05:12.420
So what's the difference to our list.

05:12.420 --> 05:17.750
Well a list once created it stops all elements of the list at the same time.

05:17.760 --> 05:24.510
And of course we can iterate over all elements but the range object arrange object never stops all elements

05:24.630 --> 05:30.610
of it at the same time so we can iterate element wise of all elements of the Range object and Python

05:30.630 --> 05:37.990
internally stores only the actual element so we iterate over one element to something that this element.

05:38.130 --> 05:44.400
Now after we finish and want to go through the next element then Python drops out our actual element

05:44.430 --> 05:50.550
and takes it out of memory and then takes the next elements or Python never stops more than one element

05:50.550 --> 05:54.640
at the same time with the Range object and that's quite memory efficient.

05:54.690 --> 06:00.720
So if you assume we don't have an element or Range object with four elements but with one million elements

06:00.720 --> 06:05.710
so it makes a difference if we store million elements like only one one element.

06:05.730 --> 06:06.150
All right.

06:06.150 --> 06:12.580
So let's go on with the Range object and of course we can create 5 iterations so we can say 40 in range.

06:12.600 --> 06:16.070
5 and 5 stands for the last element.

06:16.140 --> 06:21.300
So excluding by default our starting element is 0.

06:21.300 --> 06:28.410
So here we get all elements from 1 to 4 and we can also do it explicitly we can say we want the Range

06:28.410 --> 06:36.090
object from 0 including 2 10 excluding but only every third element so we can also define this one here

06:37.350 --> 06:40.560
and then we get 0 0 3 6 and 9.

06:40.660 --> 06:47.220
Of course we can do more than just printing we can for example multiply each element in our range object

06:47.220 --> 06:56.640
and print it out and then we get all multiplied elements from 1 to 10 including let's go to our very

06:56.700 --> 06:57.840
common workflow.

06:57.840 --> 07:05.440
So we want to store the crowd numbers from 1 to 10 in our list and therefore we create an empty list.

07:05.460 --> 07:13.500
L We iterate over the Range object the from 1 to 10 including we calculate the square number for each

07:13.500 --> 07:20.820
element and call its career and then we append each scrap number to the list ever so let's see what

07:20.820 --> 07:21.620
we get here.

07:23.850 --> 07:29.240
So we get an list with ask that numbers from 1 to 10 and that's also quite a concept.

07:29.270 --> 07:33.230
Call it the list comprehension which does actually the same top.

07:33.300 --> 07:37.300
So we can say here we create here.

07:37.440 --> 07:39.890
And the spare brackets that we create a list here.

07:39.900 --> 07:47.370
We can say please for all elements in our range Object 1 till 10 please give us the element to the power

07:47.370 --> 07:54.570
of 2 and please start in a list and it's a case execute the sale and they can see if we get the same

07:54.570 --> 07:55.530
result as before.

07:55.530 --> 08:03.130
So we reduced three lines or four lines of code to only one line of code with the list comprehension.

08:03.150 --> 08:10.020
All right so let's go on with the number case study and let's count on numbers between 0 and ten thousand

08:10.440 --> 08:14.610
that are divisible by 2 and 5 without remainder.

08:14.610 --> 08:23.560
So we can say for Aeon range ten thousand one so we want to include ten thousand and if one model or

08:23.550 --> 08:25.770
two is zero.

08:25.860 --> 08:33.930
So if one is divisible by 2 without remainder and one model of 5 equals zero then increase our count

08:33.930 --> 08:34.450
by 1.

08:34.450 --> 08:40.530
So first here at the beginning we introduce the account equal equals zero and end for each element in

08:40.530 --> 08:49.050
our range object that fulfills both conditions we have an end statement please increase our count and

08:49.050 --> 08:56.340
then we get 1001 elements that fulfill the conditions so obviously a zero for first our condition and

08:56.340 --> 09:03.730
then 10 20 30 and ten thousand so each elements that are actually divisible by 10.

09:03.780 --> 09:08.550
All right so that's for the time being and the next session we will have a look at the key arguments

09:08.550 --> 09:10.980
are key words continue break and pass.

09:10.980 --> 09:12.380
So see you there by.
